package com.isoftstone.merchant.controller.notify;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.notify.BNotify;
import com.isoftstone.merchant.entity.notify.BNotifyDetail;
import com.isoftstone.merchant.service.notice.INotifyService;
import com.isoftstone.util.Const;
import com.isoftstone.util.JsonUtil;
import com.isoftstone.util.PageData;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by as on 2017/12/5.
 * 通知管理
 */
@Controller
public class NotifyController extends BaseController{

    @Autowired
    INotifyService notifyService;
    /*
    * 到通知列表
    * */
    @RequiresPermissions("mms-menu.004.001")
    @RequestMapping("merchant/notify/getNotifyList")
    public ModelAndView getNotifyList(Page page){
        ModelAndView md=new ModelAndView();
        md.setViewName("merchant/notice/notice_manage");
        return  md;
    }
    /*
    * 到通知添加页面
    * */
    @RequestMapping("merchant/notify/addNotify")
    public ModelAndView addNotify(){
        ModelAndView md=new ModelAndView();
        md.setViewName("merchant/notice/add_notice");
        return md;
    }
    /*
    * 到通知详情页面
    * */
    @RequestMapping("merchant/notify/notifyInfo")
    public ModelAndView notifyInfo(){
        ModelAndView md=new ModelAndView();

        PageData pd = this.getPageData();

        md.addObject("NOTICE", JsonUtil.beanToJson(pd));
        md.setViewName("merchant/notice/notice_info");
        return md;
    }
    /*
    * 新增通知
    * */
    @RequestMapping(value = "merchant/notify/insertNotify",method = RequestMethod.POST)
    @ResponseBody
    //前台传入treeDemoList(集合),notifyContext
    public Object insertNotify(String treeDemos,String notifyContext){
        Tidings<String> tidings = new Tidings<String>();
        //获取页面传递的缴费用户id
        String [] treeDemoList = treeDemos.split(",");
        System.out.println(treeDemoList[0]);
        //获取当前登录用户name，当前登录用户id
        String nickname = this.getUser().getNickname();
        String corpId = this.getUser().getCorpId();
        try{
            BNotify bNotify = new BNotify();
            bNotify.setNotifyContext(notifyContext);
            bNotify.setNotifyType("04");//01：银行端管理人员 02：银行端柜员 03：缴费单位管理人员 04：缴费单位缴费用户';
            bNotify.setPhoneType(Const.n0001);//通知类型(手机端用),0001：普通通知，0002：签约通知，0003：缴费通知
            bNotify.setCreatorType("02");//创建者类型：01：银行管理人员 02：缴费单位管理人员
            bNotify.setStatus(Const.send);//1-正常 0-撤回 2-草稿
            bNotify.setCreatorUnit(corpId);//商户ID
            bNotify.setCreateDate(new Date());
            bNotify.setCreator(nickname);//当前登录用户

            BNotifyDetail bNotifyDetail = new BNotifyDetail();
            bNotifyDetail.setUser_type("03");
            bNotifyDetail.setIs_read("0");//新增时设置消息未读

            notifyService.insertNotify(treeDemoList,bNotifyDetail,bNotify);
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("新增成功！");
        }catch(Exception e){
            tidings.setStatus(Const.ERROR);
            tidings.setMsg("新增失败！");
            e.printStackTrace();
        }
        return tidings;
    }

    /*
    * 撤回(02-正常 04-撤回)
    * */
    @RequestMapping(value = "merchant/notify/withdrawNotify",method = RequestMethod.POST)
    @ResponseBody
    public Object  withdrawNotify(Long notifySeq){
        System.out.println("notifySeq = " + notifySeq);
        Tidings<String> tidings = new Tidings<String>();
        try{
            notifyService.withdrawNotify(notifySeq);
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("撤回成功！");
        }catch(Exception e){
            tidings.setStatus(Const.ERROR);
            tidings.setMsg("撤回失败！");
            e.printStackTrace();
        }
        return tidings;
    }

    /*
    * 通知列表
    * */
    @RequestMapping(value = "merchant/notify/findnotifyinfoList" ,method = RequestMethod.POST)
    @ResponseBody
    public Object findnotifyList(Page page){
        PageData pd = this.getPageData();
        Tidings<List<BNotify>> listTiding = new  Tidings<List<BNotify>> ();
        page.setPd(pd);
        try {
           // List bNotifies=new ArrayList();
            List<BNotify> bNotifies = notifyService.findnotifylistPage(page);
           /* for (BNotify bNotify : bNotifiess) {
                if(StringUtils.equals(bNotify.getStatus(),"1")){
                    bNotify.setStatus("正常");
                    bNotifies.add(bNotify);
                };
                if(StringUtils.equals(bNotify.getStatus(),"0")){
                    bNotify.setStatus("撤销");
                    bNotifies.add(bNotify);
                };
                if(StringUtils.equals(bNotify.getStatus(),"2")){
                    bNotify.setStatus("草稿");
                    bNotifies.add(bNotify);
                };
            }*/
            listTiding.setT(bNotifies).setPage(page);
            listTiding.setStatus(Const.SUCCESS);
            //List<BNotify> bNotifyList =  notifyService.findnotifylis();
        } catch (Exception e) {
            listTiding.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return listTiding ;
    }

    /*
   * 商户通知列表
   * */
    @RequestMapping(value = "merchant/notify/findMerchantNotifyList" ,method = RequestMethod.POST)
    @ResponseBody
    public Object findMerchantNotifyList(Page page){
        PageData pd = this.getPageData();
        Tidings<List<BNotify>> listTiding = new  Tidings<List<BNotify>> ();
        String cropId=this.getUser().getCorpId();
        pd.put("cropId",cropId);
        page.setPd(pd);
        try {
            List<BNotify> bNotifies = notifyService.findMervhantNotifylistPage(page);
            listTiding.setT(bNotifies).setPage(page);
            listTiding.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            listTiding.setStatus(Const.ERROR);
            e.printStackTrace();
        }
        return listTiding ;
    }
}
